AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include('shared.lua')

function ENT:Initialize()
	if (not (self.Mass)) then
		self.Entity:Remove()
	end
	self.Entity:SetModel(self.Entity.Model)
	self.Entity:PhysicsInit (SOLID_VPHYSICS)
	
	self.Entity:Activate()
	
	local phys = self.Entity:GetPhysicsObject()
	if(phys:IsValid()) then
		phys:SetMass(self.Mass)
		phys:Wake()
	end
	
	self.Inputs = Wire_CreateInputs ( self.Entity, { "Fire" } )
	self.infire = false
	
	self:SetNetworkedBool("canrender", true)
	self:SetNetworkedInt("range", self.Range)
	self:SetNetworkedVector("muzzle", self.MuzzleOffset)
	self:SetNetworkedInt("dps", self.DPS)
	
	self.minesound = CreateSound(self, "ambient/levels/citadel/zapper_loop1.wav")
	
	self:NextThink( CurTime() + math.random() )
end

function ENT:MiningTrace()
	local miningmultiplier = 1
	
	if(!self.sitp_spacetype)  then
		self.sitp_spacetype = "space"
	end
	if( self.sitp_spacetype != "space" ) then
		miningmultiplier = miningmultiplier * 0.5
		if(self.sitp_spacetype != "planet") then
			miningmultiplier = miningmultiplier * 0.1
		end
	end
	
	local muz = self.MuzzleOffset
	local pos = self.Entity:GetPos() + muz.x * self.Entity:GetForward() + muz.y * self.Entity:GetRight() + muz.z * self.Entity:GetUp()
	
	local trace = {}
		trace.start = pos + self.Entity:GetUp() * 5
		trace.endpos = pos + self.Entity:GetUp() * self.Range
		trace.filter = self.Entity 
	local tr = util.TraceLine( trace ) 
	
	if (tr.Hit && tr.Entity:IsValid()) then
		if ( tr.Entity:GetClass() == "sitp_asteroid" ) then
			tr.Entity.SITP_AsteroidHealth = math.max(tr.Entity.SITP_AsteroidHealth - self.DPS * 0.1, 0)
			tr.Entity:SetNetworkedNumber( "maxhealth", tr.Entity.SITP_AsteroidHealth )
			
			if ( SinglePlayer() == false  and self.Owner.sitp_spacetype != self.Owner.sitp_team) then
				SITP_AddCreds ( self.Owner, self.DPS * miningmultiplier * 4, true )
			end
			
			TeslaSpark(tr.HitPos, 8)
			tr.Entity:EmitSound( "ambient/energy/NewSpark0" ..tostring( math.random( 3, 5 ) ).. ".wav", true, true )
			tr.Entity.LastHit = self.Owner
		elseif ( tr.Entity:GetClass() == "debris" ) then
			if(!tr.Entity.SITP_AsteroidHealth) then
				tr.Entity.SITP_AsteroidHealth = 20
			end
			
			if(tr.Entity.SITP_AsteroidHealth > 0) then
				TeslaSpark(tr.HitPos, 8)
				tr.Entity:EmitSound( "ambient/energy/NewSpark0" ..tostring( math.random( 3, 5 ) ).. ".wav", true, true )				
				
				if ( SinglePlayer() == false and self.Owner.sitp_spacetype != self.Owner.sitp_team ) then
					SITP_AddCreds ( self.Owner, self.DPS * miningmultiplier * 4, true )
				end
				tr.Entity.SITP_AsteroidHealth = math.max(tr.Entity.SITP_AsteroidHealth - self.DPS * 0.1, 0)
			end
		end
	end
	
	local effectdata = EffectData()
		effectdata:SetMagnitude( self.DPS )
		effectdata:SetOrigin( pos + self.Entity:GetUp() * 5 )
		effectdata:SetStart( self:GetPhysicsObject():GetVelocity() )
	util.Effect( "mining_flash", effectdata )
end
	
function ENT:Think()
	if (!self.isplaysound) then
		self.isplaysound = false
	end
	if(!self.sitp_core) then
		self.sitp_core = false
	end
	if(self.sitp_core == true) then
		if (self.infire == true) then
			self:MiningTrace()
			self:SetNetworkedInt("sizeof", self.DPS)
			if(self.isplaysound == false) then
				self.minesound:Play()
				-- 255 @ 10, 200 @ 100, 55 per 90
				-- 255 - (DPS - 10) * 55 / 90
				self.minesound:ChangePitch(math.Clamp(255 - (self.DPS - 10) * 55 / 90, 0, 255), 0)
				self.isplaysound = true
			end
		else
			self:SetNetworkedInt("sizeof", 0)
			if(self.isplaysound == true) then
				self.minesound:Stop()
				self.isplaysound = false
			end
		end
	end
	self:NextThink( CurTime() + 0.05 )
end

function ENT:TriggerInput(iname, value)
	if (iname == "Fire") then
		if (value == 1 && self.infire == false) then self.infire = true end
		if (value == 0 && self.infire == true) then self.infire = false end
		return true
	end
end

function ENT:PreEntityCopy()
	//Wire dupe info
	local DupeInfo = WireLib.BuildDupeInfo(self.Entity)
	if(DupeInfo) then
		duplicator.StoreEntityModifier(self.Entity,"WireDupeInfo",DupeInfo)
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	//Wire dupe info
	if(Ent.EntityMods and Ent.EntityMods.WireDupeInfo) then
		WireLib.ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
	end

end

function ENT:OnRemove()
	Wire_Remove(self.Entity)
	self.minesound:Stop()
end

function ENT:OnRestore()
    Wire_Restored(self.Entity)
end

function TeslaSpark(pos, magnitude)
	zap = ents.Create("point_tesla")
	zap:SetKeyValue("targetname", "teslab")
	zap:SetKeyValue("m_SoundName" ,"null")
	zap:SetKeyValue("texture" ,"sprites/laser.spr")
	zap:SetKeyValue("m_Color" ,"200 200 255")
	zap:SetKeyValue("m_flRadius" ,tostring(magnitude*10))
	zap:SetKeyValue("beamcount_min" ,tostring(math.ceil(magnitude)))
	zap:SetKeyValue("beamcount_max", tostring(math.ceil(magnitude)))
	zap:SetKeyValue("thick_min", tostring(magnitude))
	zap:SetKeyValue("thick_max", tostring(magnitude))
	zap:SetKeyValue("lifetime_min" ,"0.05")
	zap:SetKeyValue("lifetime_max", "0.1")
	zap:SetKeyValue("interval_min", "0.05")
	zap:SetKeyValue("interval_max" ,"0.08")
	zap:SetPos(pos)
	zap:Spawn()
	zap:Fire("DoSpark","",0)
	zap:Fire("kill","", 0.1)
end